#include <stdio.h>
#include <assert.h>
#include <stdlib.h>

typedef int STDataType;

typedef struct Stack
{
    STDataType* a;
    int top;
    int capacity;
}Stack;

void StackInit(Stack* ps)
{
    assert(ps);

    ps->a = (STDataType*)malloc(sizeof(STDataType));
    ps->top = 0;
    ps->capacity = 0;
}

void StackDestroy(Stack* ps)
{
    assert(ps);

    free(ps->a);
    ps->a = nullptr;
    ps->top = ps->capacity = 0;
}

void StackPush(Stack* ps, STDataType data)
{
    assert(ps);

    if(ps->top == ps->capacity)
    {
        STDataType* tmp = (STDataType*)realloc(ps->a, sizeof(STDataType) * ps->capacity * 2);
        if(tmp == nullptr)
        {
            printf("realloc fail!\n");
            exit(-1);
        }
        ps->a = tmp;
        ps->capacity *= 2;
    }
    ps->a[ps->top] = data;
    ps->top++;
}

bool StackEmpty(Stack* ps)
{
    assert(ps);
    return ps->top == 0;
}

void StackPop(Stack* ps)
{
    assert(ps);
    assert(!StackEmpty(ps));

    ps->top--;
}

STDataType StackTop(Stack* ps)
{
    assert(ps);
    assert(!StackEmpty(ps));

    return ps->a[ps->top-1];
}

int StackSize(Stack* ps)
{
    assert(ps);

    return ps->top;
}